﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPlatePrintSys.Win.Controls
{
    /// <summary>
    /// BaseInfoSelectionTreeCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class BaseInfoSelectionTreeCtrl : UserControl
    {
        public static readonly DependencyProperty NodesProperty = DependencyProperty.Register(
            nameof(Nodes), typeof(ObservableCollection<BaseInfoTreeNode>), typeof(BaseInfoSelectionTreeCtrl), 
            new PropertyMetadata(null, OnNodesPropertyChanged));

        private static void OnNodesPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as BaseInfoSelectionTreeCtrl;
            if (control != null)
            {
                System.Diagnostics.Debug.WriteLine($"OnNodesPropertyChanged: NewValue={e.NewValue}, Count={((ObservableCollection<BaseInfoTreeNode>)e.NewValue)?.Count ?? 0}");
                
                // 确保DataContext正确设置，让TreeView能够绑定到Nodes属性
                control.DataContext = control;
            }
        }

        public ObservableCollection<BaseInfoTreeNode> Nodes
        {
            get => (ObservableCollection<BaseInfoTreeNode>)GetValue(NodesProperty);
            set => SetValue(NodesProperty, value);
        }

        public BaseInfoSelectionTreeCtrl()
        {
            InitializeComponent();
            
            // 设置DataContext为控件本身，这样TreeView可以绑定到Nodes属性
            this.DataContext = this;
        }

        private void LoadTestData()
        {
            //TODO 测试使用
            var root1 = new BaseInfoTreeNode("N11519");
            var a101a = new BaseInfoTreeNode("A101A", root1);
            root1.Children.Add(a101a);
            a101a.Children.Add(new BaseInfoTreeNode("7PA001", a101a));
            a101a.Children.Add(new BaseInfoTreeNode("7PA002", a101a));
            a101a.Children.Add(new BaseInfoTreeNode("7PA003", a101a));
            a101a.Children.Add(new BaseInfoTreeNode("7PA004", a101a));
            var root2 = new BaseInfoTreeNode("N11520");
            var root3 = new BaseInfoTreeNode("N11521");
            Nodes = new ObservableCollection<BaseInfoTreeNode>(new List<BaseInfoTreeNode> { root1, root2, root3 });
        }
    }
}
